﻿using System;
using System.Collections.Generic;
using System.Web;

namespace SVTTranslateTextEngine.Core
{
    public class BreakSentenceHelper
    {
        private const string SPLITE_POINT_KEY_STRING = "!！;；.。？?";

        private const string TRANS_NOT_SUPOORT_CHARS_STRING =
            "1234567890`-=~!@#$%^&*()_+\\][\';.,/》《：“”‘’、。，】【{}；<>:\"}{|\r\n\t 　";

        private const int MAX_REQUEST_SENTENCE_LENGTH = 1500;
        public const string Break_Sentences_Model_Mine = "MySelf", Break_Sentences_Model_Bing = "Bing";

        /// <summary>
        /// 斷句時所需要考慮的標點符號
        /// </summary>
        public static char[] GetSentencesSplitsKey()
        {
            return SPLITE_POINT_KEY_STRING.ToCharArray();
        }

        public static char[] GetWordsSplitsKey()
        {
            return (SPLITE_POINT_KEY_STRING + "  ").ToCharArray();
        }


        private static bool check_context_can_translate(string szContext, out string headString)
        {
            headString = "";
            foreach (char c in szContext)
            {
                if (TRANS_NOT_SUPOORT_CHARS_STRING.IndexOf(c) < 0)
                    return true;
                else
                    headString += c;
            }
            return false;
        }

        private static void check_request_sentence_length(string szContext)
        {
            if (szContext.Length > MAX_REQUEST_SENTENCE_LENGTH)
                throw new ArgumentException(
                    "Break to the shot sentence is too long to translate, please add the same point is this sentence [" +
                    HttpUtility.UrlDecode(szContext) + "]");
        }

        public static List<string> SplitSentence(string longSentence, ref List<string> splitKeyVals)
        {
            var scShotSentence = new List<string>();
            if (splitKeyVals == null) splitKeyVals = new List<string>();
            int pevEndIndex = -1;
            string tempHeadString = "";
            while (true)
            {
                string headString = "";
                int tempEndIndex = longSentence.IndexOfAny(GetSentencesSplitsKey(), pevEndIndex + 1);
                if (tempEndIndex > -1)
                {
                    string tranSrcVal = longSentence.Substring(pevEndIndex + 1, tempEndIndex - pevEndIndex);
                    if (check_context_can_translate(tranSrcVal, out headString))
                    {
                        if (headString.Length > 0)
                            tranSrcVal = tranSrcVal.Remove(0, headString.Length);
                        tranSrcVal = HttpUtility.UrlEncode(tranSrcVal);
                        check_request_sentence_length(tranSrcVal);
                        scShotSentence.Add(tranSrcVal);
                        splitKeyVals.Add(tempHeadString + headString);
                        tempHeadString = "";
                    }
                    else
                    {
                        string appVal = HttpUtility.UrlEncode(tranSrcVal);
                        tempHeadString += appVal;
                    }
                    pevEndIndex = tempEndIndex;
                }
                else
                {
                    if (pevEndIndex + 1 < longSentence.Length)
                    {
                        string val = longSentence.Substring(pevEndIndex + 1);
                        if (check_context_can_translate(val, out headString))
                        {
                            val = HttpUtility.UrlEncode(val);
                            check_request_sentence_length(val);
                            if (!string.IsNullOrEmpty(val)) scShotSentence.Add(val);
                            splitKeyVals.Add(headString);
                        }
                        else
                            splitKeyVals.Add(HttpUtility.UrlEncode(val));
                    }
                    break;
                }
            }
            return scShotSentence;
        }
    }
}